瞭解function


Posted by cmtilo on 2021-04-26

參數可以傳function進去

function transform (arr, transformFunction){
 var result = []
 for (var i =0; i<arr.length; i++) {
  result.push ( transformFunction(arr[i]))
 }
 return result
}
function double(x) {
 return x*2
}
console.log(
 transform([1,2,3], double)
) //[2,4,6]

匿名函式anonymous

function transform (arr, transformFunction){
 var result = []
 for (var i =0; i<arr.length; i++) {
  result.push ( transformFunction(arr[i]))
 }
 return result
}
console.log(
 transform([1,2,3], function (x)) {
  return x+4
 }
) //[5,6,7]

使用內建arguments可把引數一起印出來

arguments物件是一個對應傳入函式之引數的類陣列array-like物件

function add (a, b) {
 console.log(arguments) //{ '0' :2, '1' :5 } -這是一個物件
 return a+b
}
console.log (add(2, 5)) //7

#function #arguments #匿名函式 #Anonymous







Related Posts

What Type of Laser Engraving Machine Should be Used for Stainless Steel Engraving?

What Type of Laser Engraving Machine Should be Used for Stainless Steel Engraving?

React Class 與 Function component 有甚麼根本上的差別?

React Class 與 Function component 有甚麼根本上的差別?

Chapter 1. 重構:第一個範例

Chapter 1. 重構:第一個範例


Comments